package com.haha.mylife;

import java.util.List;

import org.kymjs.kjframe.http.HttpCallBack;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemLongClick;

import com.ab.activity.AbActivity;
import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.pullview.AbPullToRefreshView.OnFooterLoadListener;
import com.ab.view.pullview.AbPullToRefreshView.OnHeaderRefreshListener;
import com.haha.adapter.DelicaciesAdapter;
import com.haha.adapter.DisplayAdapter;
import com.haha.info.DelicaciesInfo;
import com.haha.info.DisplayInfo;
import com.haha.services.ApiService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class DisplayListActivity extends AbActivity
implements OnHeaderRefreshListener,OnFooterLoadListener{

	@Bind(R.id.refreshView) AbPullToRefreshView refreshView;
	@Bind(R.id.listView1) ListView listViewDel;
	
	ApiService apiService;
	DisplayAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_list);
		ButterKnife.bind(this);
		apiService=new ApiService();	
		refreshView.onHeaderRefreshFinish();
		refreshView.onFooterLoadFinish();
		refreshView.getFooterView().setVisibility(View.GONE);
		refreshView.setOnHeaderRefreshListener(this);
		refreshView.setOnFooterLoadListener(this);
		loadData();
	}

	private void loadData() {
		apiService.findAllDisplay(new HttpCallBack() {
			@Override
			public void onSuccess(String t) {
				procData(t);
			}
		});
	}

	protected void procData(String t) {
		List<DisplayInfo> list=apiService
				.procFindAllDisplay(t);
		if(adapter==null){
			adapter=new DisplayAdapter(this, list);
			listViewDel.setAdapter(adapter);
		}else{
			adapter.setList(list);
			adapter.notifyDataSetChanged();
		}
		refreshView.onHeaderRefreshFinish();
	}

	@Override
	public void onFooterLoad(AbPullToRefreshView arg0) {
		refreshView.onFooterLoadFinish();
	}

	@Override
	public void onHeaderRefresh(AbPullToRefreshView arg0) {
		loadData();
	}
	
	@OnItemLongClick(R.id.listView1) 
	boolean itemLongClick(int n){
		//Intent it=new Intent(this,DelicaciesDescActivity.class);
		//DisplayInfo di=adapter.getItem(n);
		//it.putExtra("data", di);
		//startActivityForResult(it, 0);
		return true;
	}
}
